import { http } from '@/utils/request';

export interface Dictionary {
  id: string;
  code: string;
  name: string;
  description?: string;
  status?: number;
  sort?: number;
  createdAt?: Date;
  updatedAt?: Date;
}

export interface DictionaryItem {
  id: string;
  dictionaryId: string;
  code: string;
  label: string;
  value: string;
  status?: number;
  sort?: number;
  parentId?: string;
  createdAt?: Date;
  updatedAt?: Date;
}

export interface CreateDictionaryRequest {
  code: string;
  name: string;
  description?: string;
  status?: number;
  sort?: number;
}

export interface UpdateDictionaryRequest {
  name?: string;
  description?: string;
  status?: number;
  sort?: number;
}

export interface CreateDictionaryItemRequest {
  dictionaryId: string;
  code: string;
  label: string;
  value: string;
  status?: number;
  sort?: number;
  parentId?: string;
}

export interface UpdateDictionaryItemRequest {
  code?: string;
  label?: string;
  value?: string;
  status?: number;
  sort?: number;
  parentId?: string;
}

export interface DictionaryQueryParams {
  page?: number;
  pageSize?: number;
  code?: string;
  name?: string;
  status?: number;
}

export interface DictionaryItemQueryParams {
  page?: number;
  pageSize?: number;
  dictionaryId?: string;
  code?: string;
  label?: string;
  status?: number;
  parentId?: string;
}

// 获取字典列表
export async function fetchDictionaryList(params?: DictionaryQueryParams) {
  const response = await http.get<Dictionary[]>('/system/dictionary/list', params);
  return response;
}

// 创建字典
export async function createDictionary(data: CreateDictionaryRequest) {
  const response = await http.post<Dictionary>('/system/dictionary', data);
  return response;
}

// 更新字典
export async function updateDictionary(id: string, data: UpdateDictionaryRequest) {
  const response = await http.put<Dictionary>(`/system/dictionary/${id}`, data);
  return response;
}

// 删除字典
export async function deleteDictionary(id: string) {
  const response = await http.delete<void>(`/system/dictionary/${id}`);
  return response;
}

// 获取字典项目列表
export async function fetchDictionaryItemList(params?: DictionaryItemQueryParams) {
  const response = await http.get<DictionaryItem[]>('/system/dictionary-item', params);
  return response;
}

// 创建字典项目
export async function createDictionaryItem(data: CreateDictionaryItemRequest) {
  const response = await http.post<DictionaryItem>('/system/dictionary-item', data);
  return response;
}

// 更新字典项目
export async function updateDictionaryItem(id: string, data: UpdateDictionaryItemRequest) {
  const response = await http.put<DictionaryItem>(`/system/dictionary-item/${id}`, data);
  return response;
}

// 删除字典项目
export async function deleteDictionaryItem(id: string) {
  const response = await http.delete<void>(`/system/dictionary-item/${id}`);
  return response;
}
